package com.cxs.plan.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cxs.plan.constant.MonthEnum;
import com.cxs.plan.mapper.BudgetMapper;
import com.cxs.plan.mapper.PayVariationMapper;
import com.cxs.plan.service.CollectService;
import com.cxs.plan.utils.BaseContext;
import com.cxs.plan.utils.FormatterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private PayVariationMapper payVariationMapper;

    @Autowired
    private BudgetMapper budgetMapper;

    @Override
    public JSONObject selectAppIndexInfo() {
        LocalDate currentDate = LocalDate.now();
        // 获取字段名称
        String monthKey = MonthEnum.getNowMonthKey();
        // 当前年份
        int year = currentDate.getYear();
        // 获取当天消费
        BigDecimal todayPay = payVariationMapper.selectTodayPay(BaseContext.getCurrentId());

        // 获取当月预算
        Integer budget = budgetMapper.selectMonthSurplusBudget(BaseContext.getCurrentId(), year, monthKey);
        // 获取当月消费
        JSONObject object = new JSONObject();
        String newTodayPay = todayPay == null ? BigDecimal.ZERO.toString() : FormatterUtil.formatAmount(todayPay.toString());

        object.put("todayPay", newTodayPay);
        object.put("budget", budget == null ? null : FormatterUtil.formatAmount(budget.toString()));
        return object;
    }
}
